/*
 * UIMapWindow.java
 *
 * Copyright (C) 2008 Paul Nettleship
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 */
package staticTrailExplorer;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

/**
 *
 * @author codebauer
 */
public class UIMapWindow extends javax.swing.JInternalFrame implements MouseMotionListener, MouseListener, MouseWheelListener
   {
   private static int instanceNumber = 0;
   private int lastX;
   private int lastY;
   public int activeMarker = -1;
   private UIActions UIController;
   private tileMap map;

   /** Creates a new instance of mapWindow */
   public UIMapWindow(String name, tileMap map, UIActions UIController)
      {
      super("", true, true, true, true);
      if(  map.getMapData() instanceof mapOsm  )
         setTitle("OSM Map:" + name);
      else if( map.getMapData() instanceof mapImage  )
         setTitle("Image Map:" + name);
      else if( map.getMapData() instanceof mapAtlas  )
         setTitle("Atlas Map:" + name);
      else if( map.getMapData() instanceof mapBinary  )
         setTitle("Binary Map:" + name);
      else
         setTitle("??? Map:" + name);

      this.UIController = UIController;
      this.map = map;
      UIMapPanel mapPanel = new UIMapPanel(map);
      addMouseMotionListener(this);
      addMouseListener(this);
      addMouseWheelListener(this);
      
      /* Copied from Netbeans form generated code */
      setClosable(true);
      setIconifiable(true);
      setMaximizable(true);
      setVisible(true);
      javax.swing.GroupLayout MapWindowLayout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(MapWindowLayout);
      MapWindowLayout.setHorizontalGroup(
         MapWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(mapPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
      MapWindowLayout.setVerticalGroup(
         MapWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(mapPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
      getAccessibleContext().setAccessibleName("Map");
      getAccessibleContext().setAccessibleParent(null);
      pack();
      show();

      /* Locate new window somewhere! */
      reshape((instanceNumber % 10) * 40, (instanceNumber % 40) * 40, 300, 300);
      instanceNumber++;
      }

   public tileMap getMap()
      {
      return map;
      }

   public void mouseDragged(java.awt.event.MouseEvent e)
      {
      int x = lastX - e.getX();
      int y = lastY - e.getY();
      lastX = e.getX();
      lastY = e.getY();
      UIController.moveMap( x ,y );
      UIController.repaintAll();
      }

   public void mouseMoved(java.awt.event.MouseEvent e)
      {
      if (activeMarker < 0)
         return;
      map.setMarker(activeMarker, e.getX(), e.getY());
      UIController.repaintAll();
      }

   public void mouseClicked(MouseEvent e)
      {
      activeMarker = -1;
      }

   public void mousePressed(MouseEvent e)
      {
      lastX = e.getX();
      lastY = e.getY();
      }

   public void mouseWheelMoved(MouseWheelEvent e)
      {
      UIController.changeZoom(-e.getWheelRotation());
      }

   public void mouseReleased(MouseEvent e) {  }

   public void mouseEntered(MouseEvent e) { }

   public void mouseExited(MouseEvent e) { }
   }


